package com.qf.work02;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.*;
import java.io.IOException;

@WebServlet("/login")
public class LoginServlet extends HttpServlet {
    @Override
    protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        // 解决post请求体中 ，参数中文乱码问题
        req.setCharacterEncoding("utf-8");

        final String uname = req.getParameter("uname");
        final String pwd = req.getParameter("pwd");
        String autologin = req.getParameter("autologin");


        HttpSession session = req.getSession();
        // 如果用户名是zhangsan ,密码是 123456, 则登录成功
        if (uname.equals("zhangsan")&&pwd.equals("123456")){
            session.setAttribute("user","ok");
            if ("on".equals(autologin)){
                final Cookie myc = new Cookie("myc", "666888");
                myc.setMaxAge(60*60*24*14);
                myc.setPath("/");
                resp.addCookie(myc);
            }

        }else {
            session.setAttribute("user",null);
        }

        // 思考，如何在本次登录请求中保存 用户登录成功过的状态 ？
        // 例如，如果用户名密码都正确，就把 session对象中存放一个  （key=“user”  value= User对象 ）




    }
}
